package com.example.fw;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;

public class ContactHelper extends HelperBase {

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

	public void openContactPage() {
		click(By.linkText("add new"));
	}

	public void fillContactCreationForm(ContactData contact) {
		selectByText(By.name("new_group"), contact.group);
		fillContactModificationForm(contact);
	}

	public void fillContactModificationForm(ContactData contact) {
		type(By.name("firstname"), contact.firstname);
		type(By.name("lastname"), contact.lastname);
		type(By.name("address"), contact.address1);
		type(By.name("home"), contact.mobile);
		type(By.name("home"), contact.home);
		type(By.name("mobile"), contact.mobile1);
		type(By.name("work"), contact.work);
		type(By.name("email"), contact.email);
		type(By.name("email2"), contact.email2);
		selectByText(By.name("bday"), contact.day);
		selectByText(By.name("bmonth"), contact.mouth);
		type(By.name("byear"), contact.year);
		type(By.name("address2"), contact.address2);
		type(By.name("phone2"), contact.home1);
	}	
	
	public void submitContactCreation() {
		click(By.name("submit"));
	}
	
	public void submitContactModification() {
		click(By.xpath("(//input[@name='update'])[1]"));
	}
	
	public void returnToHomePage() {
		click(By.linkText("home page"));
	}

	public void submitContactRemoving() {
		click(By.xpath("(//input[@name='update'])[2]"));
	}

	public void initContactModification(int index) {
		findElements(By.xpath("//img[@title='Edit']")).get(index).click();
	}

	public List<ContactData> getContact() {
		List<ContactData> contacts = new ArrayList<ContactData>();
		List<WebElement> checkboxes = driver.findElements(By.name("selected[]"));
		for (WebElement checkbox : checkboxes) {
			ContactData stringgroup = new ContactData();
			String title = checkbox.getAttribute("title");
			stringgroup.firstname = title.substring("select (".length(), title.length() - ")".length());
			
			contacts.add(stringgroup);
		}
		return contacts;
	}
}
